<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var longestConsecutive = function (nums) {
            if (nums.length < 2) return nums.length
            nums.sort((a, b) => a - b) // 从小到大
            let count = 1, res = 0;
            for (let i = 1; i < nums.length; i++) {
                if (nums[i] - nums[i - 1] == 1) {
                    count++
                } else if (nums[i] - nums[i - 1] == 0) {
                    continue
                } else {
                    res = Math.max(res, count)
                    count = 1
                }
            }
            console.log(nums)
            res = Math.max(res, count)
            return res
        }
        console.log(longestConsecutive([100,4,200,1,3,2]));
    </script>
</body>

</html>